import { setGlobal } from '@swda/utils'
//--------------------------------------------------------------------------
// Window_EnemyCategory
// 敌人种族分类标签

function Window_EnemyCategory() {
    this.initialize.apply(this, arguments);
}

Window_EnemyCategory.prototype = Object.create(Window_HorzCommand.prototype);
Window_EnemyCategory.prototype.constructor = Window_EnemyCategory;

Window_EnemyCategory.prototype.initialize = function() {
    Window_HorzCommand.prototype.initialize.call(this, 0, 0);
    this._windowCursorSprite.opacity = 0;
    this.refresh();
};

Window_EnemyCategory.prototype.windowWidth = function() {
    return 72*17+32;
};

Window_EnemyCategory.prototype.maxCols = function() {
    return 17;
};

Window_EnemyCategory.prototype.defaultTextColorCode = function() {
    return 0;
};

Window_EnemyCategory.prototype.selectTextColorCode = function() {
    return 6;
};

Window_EnemyCategory.prototype.drawIconItem = function(index, selected, colorCode = 0){
    const align = this.itemTextAlign();
    const rect = this.itemRectForText(index);
    rect.x -= 8;
    if (selected){
        this.drawTextEx(this.enemyClassText(index + 6)[0], rect.x, rect.y - 2)
        this.contents.adjustRectTone(100, 75, 20, rect);
        
    }else{
        this.drawTextEx(this.enemyClassText(index + 6)[0], rect.x, rect.y)
    }
   
    this.changeTextColor(this.textColor(colorCode)) // 使用自定义颜色，而不是使用黑色
    this.changePaintOpacity(this.isCommandEnabled(index));
    this.setSmallerFont();
    this.contents.fontSize = 18;
    this.drawText(this.commandName(index), rect.x + 20, rect.y+4, rect.width, align);
    this.resetFontSettings();
}

Window_EnemyCategory.prototype.drawAllItems = function() {
    const curIndex = this.index()

    var topIndex = this.topIndex();
    for (let i = 0; i < this.maxPageItems(); i++) {
        const index = topIndex + i;
        if (index < this.maxItems()) {
            const selected = index === curIndex ? true : false
            const colorCode = index === curIndex ? this.selectTextColorCode() : this.defaultTextColorCode()
            this.drawIconItem(index, selected, colorCode);
        }
    }
}


Window_EnemyCategory.prototype.updateAllItems = function() {
    if (this.contents) {
        this.contents.clear()
        this.drawAllItems()
    }
}

Window_EnemyCategory.prototype.select = function(index) {
    Window_HorzCommand.prototype.select.call(this, index)
    this.updateAllItems() // 选择时更新字体颜色
    // 刚进入时刷新
    if (this._listWindow){
        //if(this._lastIndex == undefined || this._lastIndex == -1){
        if(this._lastIndex == this._index && this._index == 0 ){
            this._listWindow.setCategory(this.index());
            this._listWindow.refresh();
            this._infoWindow.refresh();
            this._infoWindow.show();
            if (this._listWindow.index()>=0)
                this._infoWindow.setEnemy(this._listWindow._data[this._listWindow.index()]);
            else
                this._infoWindow.setEnemy(null); 
        }else if (this._index != this._lastIndex){
            this._listWindow.setCategory(this.index());
            this._listWindow.select(0);
            if (this._listWindow.index()>=0)
                this._infoWindow.setEnemy(this._listWindow._data[this._listWindow.index()]);
            else
                this._infoWindow.setEnemy(null);
        }
    }
    this._lastIndex = this._index;
}



Window_EnemyCategory.prototype.update = function() {
    Window_HorzCommand.prototype.update.call(this);

};
Window_EnemyCategory.prototype.processOk = function() {
    
};
/*
Window_EnemyCategory.prototype.refresh = function(){
    Window_HorzCommand.prototype.refresh.call(this);
    if (this._listWindow) {
        this._listWindow.setCategory(this.index());
        if (this._lastIndex != this._index)
            this._listWindow.select(0);
        if (this._listWindow.index()>=0)
            this._infoWindow.setEnemy(this._listWindow._data[this._listWindow.index()]);
        else
            this._infoWindow.setEnemy(null);
    }
    this._lastIndex = this._index;
}
*/
Window_EnemyCategory.prototype.makeCommandList = function() {
    for(var i=6; i<23; i++){
        this.addCommand($dataClasses[i].name,$dataClasses[i].name);
    }
};

Window_EnemyCategory.prototype.setListWindow = function(listWindow) {
    this._listWindow = listWindow;
};

Window_EnemyCategory.prototype.setInfoWindow = function(infoWindow) {
    this._infoWindow = infoWindow;
};

setGlobal('Window_EnemyCategory', Window_EnemyCategory)